package com.photoeditor.demo.model.face;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.photoeditor.R;
import com.photoeditor.demo.app.AppApplication;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.model.face.report.reportlogic.BaseReportLogic;
import com.photoeditor.demo.model.face.report.reportlogic.FaceAgingReportLogic;
import com.photoeditor.demo.model.face.report.reportlogic.GenderSwitchReportLogic;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.model.subscribe.manager.AppVipManager;

import java.util.ArrayList;
import java.util.List;

public class SimpleImageAdapter extends RecyclerView.Adapter {
    private final List<SimpleAdapterData> mDatas = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    private BaseReportLogic mReportLogic;

    public SimpleImageAdapter() {

    }

    public SimpleImageAdapter(BaseReportLogic reportLogic) {
        mReportLogic = reportLogic;
    }

    public void setDatas(List<SimpleAdapterData> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        mDatas.clear();
        mDatas.addAll(list);
    }

    public void addDatas(List<SimpleAdapterData> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        mDatas.addAll(list);
    }

    public void addData(SimpleAdapterData data) {
        if (data == null) {
            return;
        }
        mDatas.add(data);
    }

    public SimpleAdapterData getData(int postion) {
        if (mDatas == null || mDatas.isEmpty() || postion > (mDatas.size()-1)) {
            return null;
        }
        return mDatas.get(postion);
    }

    public int findAgeByTemplateId(String templateId) {
        if (TextUtils.isEmpty(templateId) || mDatas.isEmpty()) {
            return -1;
        }
        for(SimpleAdapterData data: mDatas) {
            if (TextUtils.equals(templateId, data.templateId)) {
                return data.age;
            }
        }
        return -1;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void removeDataByTemplateId(String templateId) {
        if (mDatas.isEmpty() || TextUtils.isEmpty(templateId)) {
            return;
        }
        int foundIndex = -1;
        int size = mDatas.size();
        for(int i=0; i<size-1; i++) {
            SimpleAdapterData data = mDatas.get(i);
            if (TextUtils.equals(data.templateId, templateId)) {
                foundIndex = i;
                break;
            }
        }
        if (foundIndex != -1) {
            mDatas.remove(foundIndex);
        }
    }

    public String getChoosenTemplateId() {
        if (mDatas.isEmpty()) {
            return null;
        }
        for(SimpleAdapterData data: mDatas) {
            if (data.selected == 1) {
                return data.templateId;
            }
        }
        return null;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.vh_simple_image, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SimpleAdapterData data = mDatas.get(position);
        LViewHolder vh = (LViewHolder)holder;
        if (data.resouceId == -1) {
            if (data.imagePath.equals(FaceConstant.getFaceModelFemale())){
                data.imagePath = "file:///android_asset/"+data.imagePath;
            }
            ImageLoader.loadImage(data.imagePath, vh.ivImage);
        } else {
            ImageLoader.loadImage(ImageLoader.resourceId2String(AppApplication.getApplication(), data.resouceId), vh.ivImage);
//            vh.ivImage.setImageResource(data.resouceId);
        }
        if ((AppVipManager.isVip() && !SPDataManager.getAdOneTimeVip()) || (data.isVip == 0)) {
            vh.ivVip.setVisibility(View.GONE);
        } else {
            vh.ivVip.setVisibility(View.VISIBLE);
        }
        vh.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mReportLogic != null) {
                    if (mReportLogic instanceof FaceAgingReportLogic) {
                        if(((FaceAgingReportLogic)mReportLogic).isLoading()) {
                            return;
                        }
                    } else if (mReportLogic instanceof GenderSwitchReportLogic) {
                        if(((GenderSwitchReportLogic)mReportLogic).isLoading()) {
                            return;
                        }
                    }
                }
                selectData(position);
                notifyDataSetChanged();
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, data, position);
                }
            }
        });
        vh.ivSelected.setVisibility(data.selected==1?View.VISIBLE:View.INVISIBLE);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void selectData(int position) {
        if (mDatas.isEmpty() || position >= mDatas.size()) {
            return;
        }
        int size = mDatas.size();
        for(int i=0; i<size; i++) {
            SimpleAdapterData data = mDatas.get(i);
            data.selected = (i==position)?1:0;
        }
    }

    public static class SimpleAdapterData {
        public String imagePath;
        public int resouceId = -1;
        public int isVip;
        public int age;
        public int selected;
        public String templateId;
    }

    class LViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImage;
        ImageView ivVip;
        ImageView ivSelected;

        public LViewHolder(View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_image);
            ivVip = itemView.findViewById(R.id.iv_vip);
            ivSelected = itemView.findViewById(R.id.iv_selected);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, SimpleAdapterData data, int postion);
    }
}
